#!python

# PyOpenSG is (C) Copyright 2005-2009 by Allen Bierbaum
#
# This file is part of PyOpenSG.
#
# PyOpenSG is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# PyOpenSG is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
# more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Python module build

# Import everything under the sun
import SCons
import SConsAddons.Options   # Get the modular options stuff
import SConsAddons.Util as sca_util      # Get the utils
import sys, os, re, string
import glob
import SCons.Defaults, SCons.Tool, SCons.Util
pj = os.path.join;

Import('*')

platform = GetPlatform()

# Get a list of all the .cpp files in the source directory
opensg_pymod_srcs = []
for path, dirs, files in sca_util.WalkBuildFromSource('.',build_env):
   #print "path: %s\n dirs:%s\n files:%s"%(path,dirs,files)
   opensg_pymod_srcs += \
      [pj(path,f) for f in files if f.endswith('.cpp') and 'osgmodule_mainheader.cpp' != f]

py_files = []
for path, dirs, files in sca_util.WalkBuildFromSource('lib',build_env):
   py_files += [pj( path,f) for f in files if f.endswith('.py')]

#print "Sources: ", opensg_pymod_srcs

# ----- Python bindings ------ #
osg_pymod_env = build_env.Copy()
#print "ccflags:", build_env["CCFLAGS"])
boost_options.applyPythonModEnv(osg_pymod_env, use_visibility=vis_option.value)
opensg_options.apply(osg_pymod_env, ["GLUT", "Cluster", "Util", "FileIO", "EffectGroups", "ContribCSM"],
                     buildType = combo["type"])
osg_pymod_env.Append(CPPPATH = ['.'], CPPDEFINES = ['BOOST_PYTHON_MAX_ARITY=21'])

# Ensure that the compiler looks for a pre-compiled header in the build directory before the
# source directory. This is necessary to get the most from GCC pre-compiled headers.
osg_pymod_env.Prepend(CPPPATH = ['..'])

# Add pre compiled headers.
if platform == "win32":
   osg_pymod_env['PCHSTOP'] = 'osg_module/osgmodule_mainheader.h'
   osg_pymod_env['PCH'] = osg_pymod_env.PCH('osgmodule_mainheader.cpp')[0]
   # NOTE: This is a workaround for an error that we get using the default
   #       located in SCons\Tool\msvc.py
   osg_pymod_env['CCPCHFLAGS'] = SCons.Util.CLVar(['${(PCH and "/Yu%s /Fp%s"%(PCHSTOP or "",PCH)) or ""}'])
else:
   # See http://www.scons.org/wiki/GchBuilder
   osg_pymod_env['GchSh'] = osg_pymod_env.GchSh('osgmodule_mainheader.h')[0]

obj_files = []
for src in opensg_pymod_srcs:
   obj = osg_pymod_env.SharedObject(source=src)
   #osg_pymod_env.Depends(obj,main_pch)
   obj_files.append(obj)

osg2_mod_dir = pj(inst_paths['python_lib'], 'osg2')

osg_module = osg_pymod_env.SharedLibrary('_osg2', obj_files)
if platform == 'win32':
   osg_module = [f for f in osg_module if str(f).endswith('.pyd')]
osg_pymod_env.Install(osg2_mod_dir, osg_module)

# Standard python files as well
py_targets = [pj(osg2_mod_dir, apply(pj, p.split(os.sep)[1:])) for p in py_files]
osg_pymod_env.InstallAs(target = py_targets, source = py_files)
